# recommendation_history/views.py
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated
from .models import RecommendationHistory
from .serializers import RecommendationHistorySerializer
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import OrderingFilter


class RecommendationHistoryViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = RecommendationHistory.objects.all()
    serializer_class = RecommendationHistorySerializer
    permission_classes = [IsAuthenticated]
    filter_backends = [DjangoFilterBackend, OrderingFilter]
    filterset_fields = ['status']
    ordering_fields = ['timestamp']
    ordering = ['-timestamp']  # 默认按时间戳降序

    def get_queryset(self):
        return self.queryset.filter(user=self.request.user)
